package com.szxy.common.redis.service.impl;

import com.szxy.common.redis.service.RedisOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * @Auther:zwer
 * @Date:2019/9/25 21:14
 * @Description:com.szxy.common.redis.service.impl
 * @Version:1.0
 **/
@Service
public class RedisOrderServiceImpl implements RedisOrderService {

    @Value("${ORDER_ITEM_ID_KEY}")
    private String ORDER_ITEM_ID_KEY;
    @Value("${INIT_ITEM_ID}")
    private String INIT_ITEM_ID;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 获取订单号
     * @return
     */
    @Override
    public Long selectOrderId() {
        Integer itemId = (Integer) this.redisTemplate.opsForValue().get(this.ORDER_ITEM_ID_KEY);
        if(itemId == null || itemId == 0){
            this.redisTemplate.opsForValue().set(this.ORDER_ITEM_ID_KEY, Long.parseLong( this.INIT_ITEM_ID));
        }
        return this.redisTemplate.opsForValue().increment(this.ORDER_ITEM_ID_KEY);

    }
}
